package com.hex.liyue.messagemodule.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hex.liyue.LiYueApplication;
import com.hex.liyue.R;
import com.hex.liyue.commonmodule.glide.GlideCircleTransform;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.util.DateUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by yukuo on 2016/5/15.
 * 这是一个会话列表的适配器
 */
public class MessageListAdapter extends RecyclerView.Adapter<MessageListAdapter.MyHolder> {
    private List<EMConversation> list;
    private OnItemClickListener onItemClickListener;
    private Activity activity;

    public MessageListAdapter(List<EMConversation> list, Activity activity) {
        this.list = list;
        this.activity = activity;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void setOnItemClickListener(String uid);
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_message, null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyHolder holder, final int position) {
        //未读数量
        if (list.get(position).getUnreadMsgCount() > 0) {
            // 显示与此用户的消息未读数
            holder.unread_msg_number.setText(String.valueOf(list.get(position).getUnreadMsgCount()));
            holder.unread_msg_number.setVisibility(View.VISIBLE);
        } else {
            holder.unread_msg_number.setVisibility(View.INVISIBLE);
        }
//            Friend friend = DaoManager.getInstance(activity).getFriend(Long.parseLong(list.get(position).getUserName()));
//            if (friend != null) {
                //头像
                Glide.with(activity).load(R.drawable.default_error_image).transform(new GlideCircleTransform(activity))
                        .into(holder.item_message_avatar);
                //名字
                holder.item_message_list_name.setText(list.get(position).getUserName());
//            }
            if (list.get(position).getAllMsgCount() != 0) {
                // 把最后一条消息的内容作为item的message内容
                EMMessage lastMessage = list.get(position).getLastMessage();
                //最后一条消息
//                holder.item_unread_message.setText(SmileUtils.getSmiledText(LiYueApplication.getInstance(), MessageUtils.getMessageDigest(lastMessage)));
                //时间
                holder.item_message_list_time.setText(DateUtils.getTimestampString(new Date(lastMessage.getMsgTime())));
            }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {

        public final RelativeLayout list_message_layout;
        public final ImageView item_message_avatar;
        public final TextView item_message_list_name;
        public final TextView item_message_list_time;
        public final TextView item_unread_message;
        public final TextView unread_msg_number;

        public MyHolder(View itemView) {
            super(itemView);
            list_message_layout = (RelativeLayout) itemView.findViewById(R.id.list_message_layout);
            item_message_avatar = (ImageView) itemView.findViewById(R.id.item_message_avatar);
            item_message_list_name = (TextView) itemView.findViewById(R.id.item_message_list_name);
            item_message_list_time = (TextView) itemView.findViewById(R.id.item_message_list_time);
            item_unread_message = (TextView) itemView.findViewById(R.id.item_unread_message);
            unread_msg_number = (TextView) itemView.findViewById(R.id.unread_msg_number);
        }
    }

}
